package io.gitee.hefren.hhmq.store;


import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.Scanner;

/**
 * @Date 2024/7/31
 * @Author lifei
 */
public class StoreDemo {

    private File createTopicFile(String fileName) {
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                boolean newFile = file.createNewFile();
                System.out.println(Strings.lenientFormat("===> %s, create file: %s", newFile, fileName));
            }
            return file;
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test01() {
        File file = createTopicFile("demo.dat");
        Path path = Paths.get(file.getAbsolutePath());
        Map<Integer, HhIndexer.Entry> indexMap = Maps.newHashMap();
        try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(path, StandardOpenOption.READ, StandardOpenOption.WRITE)) {
            MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1024 * 1024);
            // 写消息
            for (int i = 0; i < 10; i++) {
                String message = Strings.lenientFormat("%s : This is message.\n Store demo!\n", i);
                byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
                // 写数据的偏移量
                int offset = mappedByteBuffer.position();
                // 要写入的字节长度
                int len = bytes.length;
                // 存放到map中
                indexMap.put(i, new HhIndexer.Entry(offset, len));
                System.out.println(Strings.lenientFormat("===> %s: offset=%s, len=%s", i, offset, len));
                mappedByteBuffer.put(bytes);
            }
            // 读消息
            ByteBuffer byteBuffer = mappedByteBuffer.asReadOnlyBuffer();
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                if (line.equals("q")) {
                    break;
                }
                int index = Integer.parseInt(line);
                if (!indexMap.containsKey(index)) {
                    continue;
                }
                HhIndexer.Entry entry = indexMap.get(index);
                byteBuffer.position(entry.getOffset());
                byte[] bytes = new byte[entry.getLen()];
                byteBuffer.get(bytes);
                String message = new String(bytes, StandardCharsets.UTF_8);
                System.out.println(Strings.lenientFormat("==> read: %s", message));
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
